import time
import re
import json
import urllib
import scrapy
from apps.tax_policy.tax_policy.items import NetTaxPolicyItem as Item
from apps.tax_policy.tax_policy.spiders.base_spider.base_tax_policy_spider import BaseTaxPolicySpider
import math
import base64
from urllib.parse import urlencode


class spider(BaseTaxPolicySpider):
    name = "xizang_cdsbsxmzf1"

    province: str = "西藏自治区"  # 取表格
    city: str = "昌都市"  # 取表格
    county: str = "八宿县"  # 取表格
    park: str = "None"  # 取表格
    source: str = "昌都市八宿县人民政府"  # 取表格 同一个来源合并
    url: str = "https://basu.changdu.gov.cn/bsxrmzf/c101935/zfxxgk_gknrz.shtml"  # 注明入口网址，以便后续排错

    def start_requests(self):
        url = "https://basu.changdu.gov.cn/bsxrmzf/c101935/zfxxgk_gknrz.shtml"

        # 设置请求头
        headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Cache-Control': 'no-cache',
            'Connection': 'keep-alive',
            'Pragma': 'no-cache',
            'Sec-Fetch-Dest': 'document',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-Site': 'none',
            'Sec-Fetch-User': '?1',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
        }

        # 发送GET请求
        yield scrapy.Request(url=url, method='GET', headers=headers, callback=self.detail_requests)

    def detail_requests(self, response, **kwargs):
        print(response.text)
        page = response.meta.get('page', "")
        links = response.xpath("//div[@class='gknr_list']/dl/dd/a/@href").getall()  # 取链接
        title_list = response.xpath("//div[@class='gknr_list']/dl/dd/a/@title").getall()  # 取标题
        pub_date_list = response.xpath("//div[@class='gknr_list']/dl/dd/span/text()").getall()
        print("links", links, page)
        print("title_list", title_list)
        print("pub_data_list", pub_date_list)
        if len(title_list) == len(links) and len(title_list) == len(pub_date_list):
            for i in range(len(links)):
                if 'http' not in links[i]:
                    if links[i].count(".") == 5:
                        print(links[i])
                        links[i] = f"http://{links[i][5:]}"
                    if links[i].count(".") == 7:
                        print(links[i])
                        links[i] = f"http://{links[i][8:]}"
                    if links[i].count(".") == 2:
                        print(links[i])
                        links[i] = f"http://{links[i][1:]}"
                    if links[i].count(".") == 1:
                        links[i] = f"https://basu.changdu.gov.cn{links[i]}"
                print("links[i]", links[i], 'page', page)
                if 'pdf' in links[i] or 'doc' in links[i] or 'xls' in links[i] or 'jpg' in links[i]:
                    print("特殊url：", links[i])
                    d = {'url': links[i], 'title': title_list[i], 'pub_date': pub_date_list[i]}
                    self.parse_detail(None, **d)
                else:
                    yield scrapy.Request(links[i], callback=self.parse_detail,
                                         meta={'title': title_list[i], 'pub_date': pub_date_list[i]})
        else:
            raise ("出错了", page)

        # 翻页逻辑
        if response.meta.get("is_next") is not False:
            pattern = r"createPageHTML\('page-div',\s*(\d+)\s*,\s*1\s*,"
            match = re.search(pattern, response.text)
            pages = int(match.group(1))
            print("总页数：", pages)
            if pages > 1:
                for page in range(2, pages + 1):
                    print("当前页：", page)
                    not_first_url = f"https://basu.changdu.gov.cn/bsxrmzf/c101935/zfxxgk_gknrz_{page}.shtml"
                    # 设置请求头
                    headers = {
                    }

                    # 发送请求
                    yield scrapy.Request(url=not_first_url, method='GET', headers=headers,
                                         callback=self.detail_requests, meta={'is_next': False, 'page': page})

    def parse_list(self, response, **kwargs):
        pass

    def parse_detail(self, response=None, **kwargs):
        item = Item()
        if response is None:
            title = kwargs.get('title')
            pub_date = kwargs.get('pub_date')
            item['title'] = title
            item['source_url'] = kwargs.get('url')
            item['publish_date'] = pub_date
            item['content'] = kwargs.get('url')
            item['source'] = self.source
            item['province'] = self.province
            item['city'] = self.city
            item['county'] = self.county
            item['park'] = self.park
        else:
            title = response.meta.get('title')
            pub_date = response.meta.get('pub_date')
            item['title'] = title
            item['source_url'] = response.url
            item['publish_date'] = pub_date
            item['content'] = response.xpath(".").get()
            item['source'] = self.source
            item['province'] = self.province
            item['city'] = self.city
            item['county'] = self.county
            item['park'] = self.park
        # print("item", item)
        yield item


if __name__ == "__main__":
    from scrapy import cmdline

    cmdline.execute("scrapy crawl xizang_cdsbsxmzf1".split())
